<?php
/**
 * 订单管理
 * @author shitou
 *
 */
class OrderAction extends CommonAction{
	
	/**
	 * 订单列表
	 */
	public function index(){
		
		//订单状态
		$status  = C('ORDER_STATUS');
		
		if(IS_AJAX){
			
			//查询所属城市的餐厅
			if($_GET['type']=='outlet'){
				
				$map['city'] = $this->_param('city');
				$data = sendRest('BDC/Outlet/view',$map);
				
				$html = "<option value=''>请选择餐厅</option>";
				foreach($data['data'] as $k=>$v){
					$html .= "<option value='".$v['id']."'>".$v['title']."</option>";
				}
				$this->ajaxReturn($html);
			}else{
			
				$desc    = $this->_param("orderDirection","trim","desc");;
				$order   = $this->_param("orderName","trim","addtime");
				$pageNum = $this->_param("pageNum","intval",C('PAGE_NUMS'));
				$nowPage = $this->_param("nowPage","intval",1);
				$where   = $this->_param("where","trim","");
				$restid  = $this->_param("id","intval",0);
				
				if($where['start']){
					$where['start'] = strtotime($where['start']);
				}
				
				if($where['end']){
					$where['end'] = strtotime($where['end']);
				}
					
				$map   = array("where"=>$where,"pageNum"=>$pageNum, "p"=>$nowPage, "orderDirection"=>$desc ,"orderName"=>$order);
				$role  = session(C('USER_AUTH_ROLE'));
				if(C('ADMIN_ROLE')==session(C('USER_AUTH_ROLE'))){
					$map['noCheckRole'] = 1;
				}
				$json  = sendRest("OMS/Order/page",$map);
				
				$city  = C('CITYID');	
				$data  = $json['data'];
				$count = $data['count'];
				
				$totalPage = ceil($count/$pageNum);
				$jsonData  = array('page'=>$nowPage,'total'=>$count,'totalPage'=>$totalPage);
					
				foreach($data['list'] AS $row){
					
					if($row['dealid']){
						$view = M("Admin")->find($row['dealid']);
						$deal = $view['realname'];
					}
					if($row['outletid']){
						$outlet   = M('Outlet')->where("id='".$row['outletid']."'")->field('title')->find();
						$outtitle = $outlet['title'];
					}

					$time = array('2'=>'time_que','3'=>'time_fen','4'=>'time_dao','5'=>'time_pei','6'=>'time_shou');
					if($row[$time[$row['status']]]){
						$now  = '['.date('H:i',$row[$time[$row['status']]]).']';
					}else{
						$now  = '';
					}

					$orderstatus = F('orderStatus'.$role);
					$sthtml  = "当前：".$orderstatus[$row['status']]['title'];
					$sthtml .= "<br/><span dataid='".$row['id']."' class='selectStatus'>点击查看</span>";
										
					$entry = array('id'=>$row['id'],
						'cell'=>array(
							'id'           => $row['id'],
							'city'         => $city[$row['city']][1],
							'orderno'      => $row['orderno']."<input type='hidden' name='status' status='".$row['status']."' class='order_".$row['status']."' value='".$row['status']."'><br>".$outtitle,
							'status'       => $sthtml,
							'username'     => stripslashes($row['username'])."<br/>[&nbsp;".$row['mobile']."&nbsp;]",
							'mobile'       => $row['mobile'],
							'orderfee'     => $row['orderfee'],
							'cardfee'      => $row['cardfee'],
							'shipfee'      => $row['shipfee'],
							'dishesfee'    => $row['dishesfee'],
							'drinkfee'     => $row['drinkfee'],
							'b_price'      => $row['b_price'],
							'isyuding'     => $row['isyuding']==1?"是":"否",
							'dealid'       => $row['dealid']?$deal:"",
							'isinvoice'    => $row['isinvoice'] == 1 ? "是" : "否",
							'isprint'      => $row['isprint'] == 1 ? "是" : "否",
							'isconfirm'    => $row['isconfirm'] == 1 ? "是" : "否",
							'addtime'      => date('m-d H:i',$row['addtime']),
							'deal'         => createOrderStatus($row['status'],$row['id']),
						),
					);
					$jsonData['rows'][] = $entry;
				}
				$this->ajaxReturn($jsonData,"JSON");
			}
		}else{
			
			//获取可选择的配送员列表
			$map['cityid']   = session(C('USER_AUTH_CITYID'));
			$map['role']   = 3;
			$data = M("Admin")->where($map)->select();
			$this->assign("user",$data);
			
			//系统配置
			$config = F("Sysconfig");
			$this->assign('refresh',$config['cfg']['refresh']);
			
			//城市列表
			$cityarr = C('CITYID');
			$this->assign('cityarr',$cityarr);
			
			//餐厅列表
			$outlet = sendRest('BDC/Outlet/view');
			$this->assign('outlet',$outlet['data']);
			
			$this->assign("status",$status);
			$this->display();
		}
	}
	
	/**
	 * 订单备注
	 */
	public function orderbak(){
		if(IS_AJAX){
			$json = sendRest("OMS/Order/orderbak",$_POST);
			$this->ajaxReturn($json);
		}else{
			
			$id   = $this->_param("id","intval",0);
			$json = sendRest("OMS/Order/detail",array("id"=>$id));

			$this->assign("detail",$json['data']);
			$this->display();
		}
	}
	
	/**
	 * 修改订单
	 */
	public function edit(){
		
		if(IS_AJAX){
			//添加商品到订单
			if($_POST['adddetail']){
				$orderno = $_POST['orderno'];
				$val     = $_POST['val'];
				$flag    = $_POST['flag'];
				
				$data = D('Order')->addDihses($val,$flag,$orderno);
				$this->ajaxReturn($data);
			}elseif($_POST['adddrink']){
				$orderno = $_POST['orderno'];
				$val     = $_POST['val'];
				$flag    = $_POST['flag'];
				
				$data = D('Order')->addDrink($val,$flag,$orderno);
				$this->ajaxReturn($data);
			}elseif($_POST['dealbak']){
				$json = sendRest('OMS/Order/dealbak',$_POST,'post');
				$this->ajaxReturn($json);
			}elseif($_POST['editnum']){
				$data = D('Order')->editOrderDetailNum($_POST);
				$this->ajaxReturn($data);
			}else{
				
				$_POST['sdltime'] = strtotime($_POST['sdltime']);
				$_POST['edltime'] = strtotime($_POST['edltime']);
				$json = sendRest("OMS/Order/edit",$_POST,"post");
				$this->ajaxReturn($json);
			}
		}else{
			
			//订单详情
			$id = $this->_param("id","intval",0);
			$json = sendRest("OMS/Order/detail",array("id"=>$id));
			$this->assign("detail",$json['data']);
			
			//餐厅详情
			$outlet = sendRest('BDC/Outlet/detail',array('id'=>$json['data']['outletid']));
			$this->assign('outlet',$outlet['data']);
			
			//赠送的饮料
			$this->assign("sdrink",  C("IS_SEND_DRINK"));
			
			//备注信息
			$view = sendRest("OMS/Order/orderblist",array("id"=>$id));
			$this->assign("blist",$view['data']);
			
			//订单detail列表
			$map['orderno'] = $json['data']['orderno'];
			$data = sendRest("OMS/Order/dlist",$map);

			//订单地址信息
			if($json['data']['addressid']){
				$address = M('Site')->find($json['data']['addressid']);
				$this->assign('address',$address);
			}
			
			foreach($data['data'] as $k=>$v){
				$data['data'][$k]['attr'] = json_decode($v['attr'],true);
				if($v['flag']=="dishes"){
					$cat['id'] = $v['pid'];
					$view = sendRest("RMS/Product/dishesDetail",$cat);
					$data['data'][$k]['detail']         = $view['data'];
					$data['data'][$k]['detail']['gnfr'] = json_decode($view['data']['gnfr'],true);
					$data['data'][$k]['detail']['must'] = D('Order')->checkAttr($view['data']['gnfr']);
				}
			}
		
			$this->assign("dlist",$data['data']);
			$this->assign("id",$id);
		}
		$this->display();
	}

	/**
	 * 查看订单
	 */
	public function view(){
		
		//订单详情
		$id = $this->_param("id","intval",0);
		$json = sendRest("OMS/Order/detail",array("id"=>$id));
		$this->assign("detail",$json['data']);
			
		//赠送的饮料
		$this->assign("sdrink",  C("IS_SEND_DRINK"));
		
		//订单备注信息
		$view = sendRest("OMS/Order/orderblist",array("id"=>$id));
		$this->assign("blist",$view['data']);
		
		//订单detail列表
		$map['orderno'] = $json['data']['orderno'];
		$data = sendRest("OMS/Order/dlist",$map);
		
		foreach($data['data'] as $k=>$v){
			$data['data'][$k]['attr'] = json_decode($v['attr'],true);
			if($v['flag']=="dishes"){
				$cat['id'] = $v['pid'];
				$view = sendRest("RMS/Product/dishesDetail",$cat);
				$data['data'][$k]['detail']         = $view['data'];
				$data['data'][$k]['detail']['gnfr'] = json_decode($view['data']['gnfr'],true);
			}
		}
		$this->assign("dlist",$data['data']);
		$this->assign("id",$id);
		$this->display();
	}
	
	/**
	 * 订单详情
	 */
	public function detail(){
		
		$map['id'] = $this->_param("id","intval",0);
		$json = sendRest("OMS/Order/detail",$map);
		
		$this->assign("detail",$json['data']);
		$this->display();
	}

	//正在确认
	public function order_confirm(){
		$map['id']     = $this->_param("id","intval",0);
		$map['status'] = 2;
		$data = M('Order')->save($map);
		if($data){
			$map['remark'] = "订单正在确认";
			creatOrderLog($map);
			$this->ajaxReturn(array('status'=>1));
		}else{
			$this->ajaxReturn(array('status'=>-1,'info'=>'订单状态修改失败，请稍后重试!'));
		}
	}
	
	//已确认
	public function order_isconfirm(){
		$map['id']     = $this->_param("id","intval",0);
		$map['status'] = 3;
		$map['time_que'] = time();
		$data = M('Order')->save($map);
		if($data){
			$map['remark'] = "订单已确认";
			creatOrderLog($map);
			$this->ajaxReturn(array('status'=>1));
		}else{
			$this->ajaxReturn(array('status'=>-1,'info'=>'订单状态修改失败，请稍后重试!'));
		}
	}
	
	//已报餐
	public function order_notify(){
		$map['id']     = $this->_param("id","intval",0);
		$map['status'] = 4;
		$data = M('Order')->save($map);
		if($data){
			$map['remark'] = "订单已报餐";
			creatOrderLog($map);
			$this->ajaxReturn(array('status'=>1));
		}else{
			$this->ajaxReturn(array('status'=>-1,'info'=>'订单状态修改失败，请稍后重试!'));
		}
	}
	
	//分配订单
	public function order_fenpei(){
		$model = D("Order");
		
		if(IS_AJAX){
			$map['id']      = $this->_param("id","intval",0);
			$map['dealid']  = $this->_param("dealid","intval",0);
			$map['orderno'] = $this->_param("orderno","trim","");
			$json = sendRest("OMS/Order/deal",$map,'post');
			
			//发送短信
			if($json['status']==1){
				
				//操作员详情
				$admin = M('Admin')->find($map['dealid']);
				$map['remark'] = "订单分配成功，配送员：".$admin['realname'];
				creatOrderLog($map);
				
				//发送短信给配送员
    			$model->sendSms($map);
			}
			$this->ajaxReturn($json);
		}else{
			
			$id   = $this->_param("id","intval",0);
			$json = sendRest("OMS/Order/detail",array("id"=>$id));
			$this->assign("detail",$json['data']);
			
			//获取可选择的配送员列表
			$map['cityid']   = session(C('USER_AUTH_CITYID'));
			$map['role']   = 3;
			$data = M("Admin")->where($map)->select();
			$this->assign("user",$data);
			$this->display();
		}
	}
	
	//需要修改
	public function order_neededit(){
		
		//订单详情
		$id   = $this->_param("id","intval",0);
		$json = sendRest("OMS/Order/detail",array("id"=>$id));
		
		if(IS_POST){
			$map['id']      = $id;
			$map['status']  = 6;
			$data = M('Order')->save($map);
			if($data){
				$map['remark'] = "订单需要修改，修改原因：".$_POST['remark'];
				creatOrderLog($map);
				$this->ajaxReturn(array('status'=>1));
			}else{
				$this->ajaxReturn(array('status'=>-1,'info'=>'订单状态修改失败，请稍后重试!'));
			}
		}else{
			
			$this->assign("detail",$json['data']);
			$this->display();
		}
	}
	
	//已修改
	public function order_isedit(){
		$map['id']     = $this->_param("id","intval",0);
		$map['status'] = 7;
		$data = M('Order')->save($map);
		if($data){
			$map['remark'] = "订单已修改";
			creatOrderLog($map);
			$this->ajaxReturn(array('status'=>1));
		}else{
			$this->ajaxReturn(array('status'=>-1,'info'=>'订单状态修改失败，请稍后重试!'));
		}
	}
	
	//餐厅已受理
	public function order_editok(){
		$map['id']     = $this->_param("id","intval",0);
		$map['status'] = 8;
		$data = M('Order')->save($map);
		if($data){
			$map['remark'] = "餐厅已受理最新的修改需求";
			creatOrderLog($map);
			$this->ajaxReturn(array('status'=>1));
		}else{
			$this->ajaxReturn(array('status'=>-1,'info'=>'订单状态修改失败，请稍后重试!'));
		}
	}
	
	//投诉中
	public function order_complaint(){
		//订单详情
		$id   = $this->_param("id","intval",0);
		$json = sendRest("OMS/Order/detail",array("id"=>$id));
		
		if(IS_AJAX){
			$map['id']     = $id;
			$map['status'] = 12;
			$data = M('Order')->save($map);
			if($data){
				$map['remark'] = "客人投诉订单，投诉正在处理。投诉原因：".$_POST['remark'];
				creatOrderLog($map);
				$this->ajaxReturn(array('status'=>1));
			}else{
				$this->ajaxReturn(array('status'=>-1,'info'=>'订单状态修改失败，请稍后重试!'));
			}
		}else{
			$this->assign("detail",$json['data']);
			$this->display();
		}
	}
	
	//投诉已处理
	public function order_iscomplaint(){
		//订单详情
		$id   = $this->_param("id","intval",0);
		$json = sendRest("OMS/Order/detail",array("id"=>$id));
		
		if(IS_AJAX){
			$map['id']     = $id;
			$map['status'] = 13;
			$data = M('Order')->save($map);
			if($data){
				$map['remark'] = "客人投诉订单，投诉已处理完毕。处理结果：".$_POST['remark'];
				creatOrderLog($map);
				$this->ajaxReturn(array('status'=>1));
			}else{
				$this->ajaxReturn(array('status'=>-1,'info'=>'订单状态修改失败，请稍后重试!'));
			}
		}else{
			$this->assign("detail",$json['data']);
			$this->display();
		}
	}
	
	//取消订单
	public function order_cancel(){
		$map['id']     = $this->_param("id","intval",0);
		$map['status'] = -1;
		$data = M('Order')->save($map);
		if($data){
			$map['remark'] = "订单被取消";
			creatOrderLog($map);
			$this->ajaxReturn(array('status'=>1));
		}else{
			$this->ajaxReturn(array('status'=>-1,'info'=>'订单状态修改失败，请稍后重试!'));
		}
	}
	
	//订单关闭
	public function order_over(){
		$map['id']     = $this->_param("id","intval",0);
		$map['status'] = 14;
		$data = M('Order')->save($map);
		if($data){
			$map['remark'] = "订单已完成";
			creatOrderLog($map);
			$this->ajaxReturn(array('status'=>1));
		}else{
			$this->ajaxReturn(array('status'=>-1,'info'=>'订单状态修改失败，请稍后重试!'));
		}
	}
	
	//查看订单状态
	public function order_status(){
		
		$map['oldid'] = $this->_param('id','intval',0);
		$list = M('OrderLog')->where($map)->order('addtime desc')->select();
		
		$this->assign('list',$list);
		$this->display();
	}
	
	/**
	 * 删除订单
	 */
	public function del(){
		
		$map['val'] = $this->_param("val","trim","");
		$json = sendRest("OMS/Order/del",$map);
		
		$this->ajaxReturn($json);
	}
	
	/**
	 * 取消订单
	 */
	public function cancel(){
		
		$map['val'] = $this->_param('val','trim','');
		$json = sendRest('OMS/Order/cancel',$map,'post');
		$this->ajaxReturn($json);
	}
	
	/**
	 * 客服确认订单
	 */
	public function confirm(){
		$map['val'] = $this->_param("val","trim","");
		$json = sendRest("OMS/Order/confirm",$map);
		
		$this->ajaxReturn($json);
	}
	
	/**
	 * 分配订单
	 */
	public function deal(){
		
		$model = D("Order");
		
		if(IS_AJAX){
			
			$map['id']      = $this->_param("id","intval",0);
			$map['dealid']  = $this->_param("dealid","intval",0);
			$map['orderno'] = $this->_param("orderno","trim","");
			$json = sendRest("OMS/Order/deal",$map);
			
			//发送短信
			if($json['status']==1){
    			$model->sendSms($map);
			}
			$this->ajaxReturn($json);
		}else{
			
			$id   = $this->_param("id","intval",0);
			$json = sendRest("OMS/Order/detail",array("id"=>$id));
			$this->assign("detail",$json['data']);
			
			//获取可选择的配送员列表
			$map['cityid']   = session(C('USER_AUTH_CITYID'));
			$map['role']   = 3;
			$data = M("Admin")->where($map)->select();
			
			$this->assign("user",$data);
			$this->display();
		}
	}
	
	/**
	 * 订单状态报表
	 */
	public function status(){
		$model  = D("Order");
		$cityid = session(C('USER_AUTH_CITYID'));
		
		$m['startTime'] = $this->_param("startTime","trim","");
		$m['endTime']   = $this->_param("endTime",  "trim","");
		$m['dealid']    = $this->_param("dealid","intval",0);
		$m['orderby']   = $this->_param("orderby","trim","addtime desc");
		$m['cityid']    = $this->_param("cityid")?$this->_param("cityid"):$cityid;
		
		if($m['endTime']||$m['startTime']){//根据开始结束时间进行搜索
			if($m['startTime']&&$m['endTime']){
				$start = strtotime($m['startTime']);
				$end   = strtotime($m['endTime']);
				$map['addtime'] = array(array("egt",$start),array("elt",$end));
			}else{
				if($m['startTime']){
					$start = strtotime($m['startTime']);
					$map['addtime'] = array("egt",$start);
				}
				if($m['endTime']){
					$end = strtotime($m['endTime']);
					$map['addtime'] = array("elt",$end);
				}
			}
		}
		if($m['dealid']){
			$map['dealid'] = $m['dealid'];
		}
		if($m['cityid']){
			$map['city'] = $m['cityid'];
		}
		$map['status'] = array(array('eq',5),array('eq',6),'or');
		
		$desc    = $this->_param("orderDirection","trim","desc");;
		$order   = $this->_param("orderName","trim","addtime");
		$pageNum = $this->_param("pageNum","intval",6);
		$nowPage = $this->_param("p","intval",1);
		$where   = $this->_param("where","trim","");
		$restid  = $this->_param("id","intval",0);
		
		$list = $model->where($map)->order($m['orderby'])
		->limit((intval($nowPage)-1)*$pageNum,$pageNum)->select();
		$row  = $model->where($map)->select();
		$this->assign("list",$list);
		
		//获取统计数据
		$total = $model->getStatusTotal($row);
		$this->assign("total",$total);
		
		//获取可选择的配送员列表
		$arr['city']   = session(C('USER_AUTH_CITYID'));
		$arr['role']   = 3;
		$data = M("Admin")->where($arr)->select();
		$this->assign("user",$data);
		
		if($pageNum == count($list)){
			$m['p'] = $nowPage+1;
			$this->assign("param",http_build_query($m));
		}else{
			$this->assign("empty","<tr><td align='center' colspan='8'>没有找到相关的数据</td></tr>");
		}
		if(IS_AJAX){
			exit($this->fetch("ajaxstatus"));
		}
		
		$this->assign("m",$m);
		$this->display();
	}
	
	/**
	 * 订单时间报表
	 */
	public function time(){
		
		$model  = D("Order");
		$cityid = session(C('USER_AUTH_CITYID'));
	
		$m['startTime'] = $this->_param("startTime","trim","");
		$m['endTime']   = $this->_param("endTime",  "trim","");
		$m['dealid']    = $this->_param("dealid","intval",0);
		$m['orderby']   = $this->_param("orderby","trim","addtime desc");
		$m['cityid']    = $this->_param("cityid")?$this->_param("cityid"):$cityid;
	
		if($m['endTime']||$m['startTime']){//根据开始结束时间进行搜索
			if($m['startTime']&&$m['endTime']){
				$start = strtotime($m['startTime']);
				$end   = strtotime($m['endTime']);
				$map['addtime'] = array(array("egt",$start),array("elt",$end));
			}else{
				if($m['startTime']){
					$start = strtotime($m['startTime']);
					$map['addtime'] = array("egt",$start);
				}
				if($m['endTime']){
					$end = strtotime($m['endTime']);
					$map['addtime'] = array("elt",$end);
				}
			}
		}
		if($m['dealid']){
			$map['dealid'] = $m['dealid'];
		}
		if($m['cityid']){
			$map['city'] = $m['cityid'];
		}
		
		$map['status'] = array(array("eq",5),array("eq",6),'OR');
	
		$pageNum = $this->_param("pageNum","intval",10);
		$nowPage = $this->_param("p","intval",1);
		$where   = $this->_param("where","trim","");
		$restid  = $this->_param("id","intval",0);
	
		$list = $model->where($map)->order($m['orderby'])
		->limit((intval($nowPage)-1)*$pageNum,$pageNum)->select();
		
		//查询餐厅名称、快递员姓名
		foreach($list as $k=>$v){
			$outletName = M('Outlet')->find($v['outletid']);
			$list[$k]['outletName'] = $outletName['title'];
		}
		$this->assign("list",$list);
		if($pageNum == count($list)){
			$m['p'] = $nowPage+1;
			$this->assign("param",http_build_query($m));
		}else{
			$this->assign("empty","<tr><td align='center' colspan='12'>没有找到相关的数据</td></tr>");
		}
		if(IS_AJAX){
			exit($this->fetch("ajaxtime"));
		}
		
		//获取可选择的配送员列表
		$arr['city']   = session(C('USER_AUTH_CITYID'));
		$arr['role']   = 3;
		$data = M("Admin")->where($arr)->select();
		$this->assign("user",$data);
	
		$this->assign("m",$m);
		$this->display();
	}
	
	/**
	 * 导出订单时间报表
	 */
	public function export_time(){
		$model  = D("Order");
		$cityid = session(C('USER_AUTH_CITYID'));
		
		$m['startTime'] = $this->_param("startTime","trim","");
		$m['endTime']   = $this->_param("endTime",  "trim","");
		$m['dealid']    = $this->_param("dealid","intval",0);
		$m['orderby']   = $this->_param("orderby","trim","addtime desc");
		$m['cityid']    = $this->_param("cityid")?$this->_param("cityid"):$cityid;
		
		if($m['startTime']){
			$start = strtotime($m['startTime']);
			$map['addtime'] = array("egt",$start);
		}
		if($m['endTime']){
			$end = strtotime($m['endTime']);
			$map['addtime'] = array("elt",$end);
		}
		if($m['dealid']){
			$map['dealid'] = $m['dealid'];
		}
		if($m['cityid']){
			$map['city'] = $m['cityid'];
		}
		
		$map['status'] = array(array('eq',5),array('eq',6),'or');
		
		$map['status'] = array(array("eq",5),array("eq",6),'OR');
		$list = $model->where($map)->order($m['orderby'])->select();
		
		//获取可选择的配送员列表
		$rows['city']   = session(C('USER_AUTH_CITYID'));
		$rows['role']   = 3;
		$user = M("Admin")->where($rows)->select();
		
		$model->export_time($list,$user);
	}
	
	/**
	 * 导出订单销售报表
	 */
	public function export_sell(){
		$model  = D("Order");
		$cityid = session(C('USER_AUTH_CITYID'));
		
		$m['startTime'] = $this->_param("startTime","trim","");
		$m['endTime']   = $this->_param("endTime",  "trim","");
		$m['dealid']    = $this->_param("dealid","intval",0);
		$m['orderby']   = $this->_param("orderby","trim","addtime desc");
		$m['cityid']    = $this->_param("cityid")?$this->_param("cityid"):$cityid;
		
		if($m['endTime']||$m['startTime']){//根据开始结束时间进行搜索
			if($m['startTime']&&$m['endTime']){
				$start = strtotime($m['startTime']);
				$end   = strtotime($m['endTime']);
				$map['addtime'] = array(array("egt",$start),array("elt",$end));
			}else{
				if($m['startTime']){
					$start = strtotime($m['startTime']);
					$map['addtime'] = array("egt",$start);
				}
				if($m['endTime']){
					$end = strtotime($m['endTime']);
					$map['addtime'] = array("elt",$end);
				}
			}
		}
		if($m['dealid']){
			$map['dealid'] = $m['dealid'];
		}
		if($m['cityid']){
			$map['city'] = $m['cityid'];
		}
		//$map['status'] = array(array('eq',5),array('eq',6),'or');
		$map['status']   = 14;
		
		$desc    = $this->_param("orderDirection","trim","desc");;
		$order   = $this->_param("orderName","trim","addtime");
		$pageNum = $this->_param("pageNum","intval",6);
		$nowPage = $this->_param("p","intval",1);
		$where   = $this->_param("where","trim","");
		$restid  = $this->_param("id","intval",0);
		
		$list = $model->where($map)->order($m['orderby'])->select();
		
		$model->export_sell($list);
	}
	
	/**
	 * 订单销售报表
	 */
	public function sell(){
		
		$model  = D("Order");
		$cityid = session(C('USER_AUTH_CITYID'));

		$m['startTime'] = $this->_param("startTime","trim","");
		$m['endTime']   = $this->_param("endTime",  "trim","");
		$m['dealid']    = $this->_param("dealid","intval",0);
		$m['orderby']   = $this->_param("orderby","trim","addtime desc");
		$m['cityid']    = $this->_param("cityid")?$this->_param("cityid"):$cityid;
		
		if($m['endTime']||$m['startTime']){//根据开始结束时间进行搜索
			if($m['startTime']&&$m['endTime']){
				$start = strtotime($m['startTime']);
				$end   = strtotime($m['endTime']);
				$map['addtime'] = array(array("egt",$start),array("elt",$end));
			}else{
				if($m['startTime']){
					$start = strtotime($m['startTime']);
					$map['addtime'] = array("egt",$start);
				}
				if($m['endTime']){
					$end = strtotime($m['endTime']);
					$map['addtime'] = array("elt",$end);
				}
			}
		}
		if($m['dealid']){
			$map['dealid'] = $m['dealid'];
		}
		if($m['cityid']){
			$map['city'] = $m['cityid'];
		}
		//$map['status'] = array(array('eq',6),array('eq',7),'or');
		$map['status']   = 14;
		
		$desc    = $this->_param("orderDirection","trim","desc");;
		$order   = $this->_param("orderName","trim","addtime");
		$pageNum = $this->_param("pageNum","intval",6);
		$nowPage = $this->_param("p","intval",1);
		$where   = $this->_param("where","trim","");
		$restid  = $this->_param("id","intval",0);
		
		$list = $model->where($map)->order($m['orderby'])
		->limit((intval($nowPage)-1)*$pageNum,$pageNum)->select();
		
		//查询餐厅名称
		foreach($list as $k=>$v){
			$outletName = M('Outlet')->find($v['outletid']);
			$list[$k]['outletName'] = $outletName['title'];
		}
	
		$row  = $model->where($map)->select();
		$this->assign("list",$list);
		
		//获取统计数据
		$total = $model->getSellTotal($row);
		$this->assign("total",$total);
		
		//地址列表
		$data = sendRest("BDC/Outlet/dictAddress");
		$this->assign("address",$data['data']['zh-cn']['array']);
		
		//获取可选择的配送员列表
		$arr['cityid']   = session(C('USER_AUTH_CITYID'));
		$arr['role']   = 3;
		$data = M("Admin")->where($arr)->select();
		$this->assign("user",$data);
		
		if($pageNum == count($list)){
			$m['p'] = $nowPage+1;
			$this->assign("param",http_build_query($m));
		}else{
			$this->assign("empty","<tr><td align='center' colspan='8'>没有找到相关的数据</td></tr>");
		}
		if(IS_AJAX){
			exit($this->fetch("ajax"));
		}
		
		$this->assign("m",$m);
		$this->display();
	}
	
	/**
	 * 
	 */
	public function delivery(){
		
		//获取配送员列表
		$map['status'] = 1;
		//$map['city']   = 1;
		$map['role']   = 9;
		$data = M("User")->where($map)->select();
		$this->assign("user",$data);
		
		
		
		$model = D("Order");
		
		$m['end']    = $this->_param("endTime",  "trim","");
		$m['start']  = $this->_param("startTime","trim","");
		$m['dealid'] = $this->_param("dealid","intval",0);
		
		if($m['end']){
			$map['end'] = strtotime($m['end']);
		}
		
		if($m['start']){
			$map['start'] = strtotime($m['start']);
		}
		
		if($m['dealid']){
			$map['dealid'] = $m['dealid'];
		}
		//获取订单列表
		$json = sendRest("OMS/Order/view",$map);
		
		//获取统计数据
		$total = $model->getDeliveryTotal($json['data'],$m['dealid']);
		$this->assign("total",$total);
		
		//检索配送员
		$list = $model->checkDelivery($json['data'],$data);
		$this->assign("list",$list);
		
		$this->assign("m",$m);
		$this->display();
	}
	
	/**
	 * 导出订单
	 */
	public function export(){
		
		$model = D("Order");
		$where['keywords'] = $this->_param("keywords","trim", "");
		$where['status']   = $this->_param("status","intval", 0);
		$where['dealid']   = $this->_param("dealid","intval", 0);
		$where['city']     = $this->_param("city","intval", 0);
		$where['outletid'] = $this->_param("outletid","intval", 0);
		$where['start']    = $this->_param("startTime", "trim",   '');
		$where['end']      = $this->_param("endTime",   "trim",   '');
		
		if($where['keywords']){
    		$map['keywords'] = $where['keywords'];
    	}
    	if($where['status']){
    		$map['status'] = $where['status'];
    	}
    	if($where['dealid']){
    		$map['dealid'] = $where['dealid'];
    	}
    	if($where['outletid']){
    		$map['outletid'] = $where['outletid'];
    	}
    	if($where['city']){
    		$map['city'] = $where['city'];
    	}
    	if($where['start']){
    		$map['start'] = strtotime($where['start']);
    	}
    	if($where['end']){
    		$map['end'] = strtotime($where['end']);
    	}
		
		$map['status'] = array('neq','-1');
    	$json  = sendRest("OMS/Order/view",$map,'post');
    	$list  = $json['data'];
    	
    	foreach($list as $k=>$v){
    		if($v['dealid']){
    			$view = M("Admin")->find($v['dealid']);
    			$list[$k]['realname'] = $view['realname'];
    		}
    	}
    	$model->export($list);
	}
	
	//导出财务报表
	public function exportCaiwu(){
		$model = D("Order");
		$where['keywords'] = $this->_param("keywords","trim", "");
		$where['status']   = $this->_param("status","intval", 0);
		$where['dealid']   = $this->_param("dealid","intval", 0);
		$where['city']     = $this->_param("city","intval", 0);
		$where['outletid'] = $this->_param("outletid","intval", 0);
		$where['start']    = $this->_param("startTime", "trim",   '');
		$where['end']      = $this->_param("endTime",   "trim",   '');
		
		if($where['keywords']){
    		$map['keywords'] = $where['keywords'];
    	}
    	if($where['status']){
    		$map['status'] = $where['status'];
    	}
    	if($where['dealid']){
    		$map['dealid'] = $where['dealid'];
    	}
    	if($where['outletid']){
    		$map['outletid'] = $where['outletid'];
    	}
    	if($where['city']){
    		$map['city'] = $where['city'];
    	}
    	if($where['start']){
    		$map['start'] = strtotime($where['start']);
    	}
    	if($where['end']){
    		$map['end'] = strtotime($where['end']);
    	}
		
		$map['status'] = array('neq','-1');
    	$json  = sendRest("OMS/Order/view",$map,'post');
    	$list  = $json['data'];
    	
    	foreach($list as $k=>$v){
    		if($v['dealid']){
    			$view = M("Admin")->find($v['dealid']);
    			$list[$k]['realname'] = $view['realname'];
    		}
    	}
		
		$model->exportCaiwu($list);
	}
	
	//获取配送员
	function getDelivery(){
		$cityid = $this->_param("cityid","trim","");
		$map['status'] = 1;
		$map['city']   = $cityid;
		$map['role']   = 9;
		$data = M("User")->where($map)->select();
		
		echo json_encode($data);
	}
	
	/**
	 * 删除某订单中的某个商品
	 */
	public function delProduct(){
		
		$map['id']  = $this->_param("id","intval",0);
		$map['oid'] = $this->_param("oid","intval",0);
		
		$json = sendRest("OMS/Order/delProduct",$map);
		$this->ajaxReturn($json);
	}
	
	/**
	 * 打印订单
	 */
	public function printOrder(){
		
		//订单信息
		$id = $this->_param("id","intval",0);
		$json = sendRest("OMS/Order/detail",array("id"=>$id));
		$this->assign("detail",$json['data']);
		
		//会员信息
		$query = sendRest('OMS/Order/order_select',array('dataid'=>$json['data']['dataid']));
		foreach($query['data'] as $k=>$v){
			$total += $v['orderfee'];
		}
		$this->assign('total',$total);
		
		//订单备注信息
		$addid    = $json['data']['id'];
		$orderbak = sendRest("OMS/Order/orderbakList",array('id'=>$addid));
		$this->assign("orderbak",$orderbak['data']);
		
		//餐厅信息
		$outletid = $json['data']['outletid'];
		$outlet   = sendRest("BDC/Outlet/detail",array("id"=>$outletid));
		$this->assign("outlet",$outlet['data']);
		
		//订单详情信息
		$map['orderno'] = $json['data']['orderno'];
		$data = sendRest("OMS/Order/dlist",$map);
		
		$totalPrice = 0;
		foreach($data['data'] as $k=>$v){
			$totalPrice += $v['price']*$v['amount'];
			$data['data'][$k]['attr'] = json_decode($v['attr'],true);
			if($v['flag']=="dishes"){
				$cat['id'] = $v['pid'];
				$view = sendRest("RMS/Product/dishesDetail",$cat);
				$data['data'][$k]['detail']         = $view['data'];
				$data['data'][$k]['detail']['gnfr'] = json_decode($view['data']['gnfr'],true);
			}
		}
		
		$this->assign("totalPrice",$totalPrice);
		$this->assign("dlist",$data['data']);
		$this->assign("id",$id);
		
		$this->display();
	}
	
	//导出pdf
	public function printpdf(){
		vendor('Tcpdf.tcpdf');
		$pdf=new Tcpdf();
		
		$id  = $this->_param('id');
		$html = D('Order')->getHtml($id);
		//$html = $this->fetch('pdf1');
		
		$pdf->SetCreator(PDF_CREATOR);
		$pdf->SetAuthor('Nicola Asuni');
		$pdf->SetTitle('TCPDF Example 038');
		$pdf->SetSubject('TCPDF Tutorial');
		$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
		$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 038', PDF_HEADER_STRING);
		$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
		$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
		$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
		$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
		$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
		$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
		$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
		$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
		if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
			require_once(dirname(__FILE__).'/lang/eng.php');
			$pdf->setLanguageArray($l);
		}
		$pdf->SetFont('helvetica', '', 20);
		$pdf->AddPage();
		$txt = '';
		$pdf->Write(0, $txt, '', 0, 'L', true, 0, false, false, 0);
		$pdf->SetFont('stsongstdlight', '', 10);
		$txt = 'こんにちは世界';
		//$pdf->WriteHtml(0, $htm, '', 0, 'L', true, 0, false, false, 0);
		$pdf->writeHTML($html, true, false, true, false, '');
		$pdf->Output('example_038.pdf', 'I');
	}
	
	/**
	 * 客服通过客户电话添加订单
	 */
	public function add(){
		
		$model = D('Order');
		$type  = $this->_param('type');
		
		if(IS_AJAX){
			
			if($type=='member'){  //查询会员列表
				$map['keywords'] = $this->_param('keywords','trim','');
				$data = sendRest('MMS/Custom/select',$map);
				
				$html = $model->getMemberSelect($data);
				$this->ajaxReturn($html);
			}
			if($type=='mdetail'){  //查询会员详情
				
				$id   = $this->_param('id','intval',0);
				$data = $model->getMemberDetail($id);
				
				$this->ajaxReturn($data);
			}
			if($type=='memberads'){ //查询会员地址
				$map['city']   = $this->_param('cityid','intval',0);
				$map['userid'] = $this->_param('memberid','intval',0);
				$map['isdel']  = 2;
				
				$data = $model->getMemberAddress($map);
				$this->ajaxReturn($data);
			}
			if($type=='adsdetail'){ //查询地址详情
				$id     = $this->_param('id','intval',0);
				$userid = $this->_param('userid','intval',0);
				if($id==-1){
					$data = M('Member')->find($userid);
				}else{
					$data = sendRest('RMS/Address/detail',array('id'=>$id));
					$data = $data['data'];
				}
				$this->ajaxReturn($data);
			}
			if($type=="cuisine"){  //获取菜系列表
				$map['cityid'] = $this->_param('cityid','intval',0);
				$data =  sendRest("BDC/Cuisine/view",$map);
				
				$html = "<option value=''>请选择菜系</option>";
				foreach($data['data'] as $k=>$v){
					$html .= "<option value='".$v['id']."'>".$v['title']."</option>";
				}
				
				$this->ajaxReturn($html);
			}
			if($type=='restaurant'){  //查询餐厅列表
				$map['city']     = $this->_param('cityid','intval',0);
				$map['cuisine']  = $this->_param('cuisine','intval',0);
				$map['keywords'] = $this->_param('keywords','trim','');
				$map['tingye']   = 1;
				$data = sendRest("BDC/Outlet/view",$map);
				
				$html = "<option value=''>请选择餐厅</option>";
				foreach($data['data'] as $k=>$v){
					$css   = $model->getRestaurantTime($v);
					$html .= "<option style='$css' value='".$v['id']."'>".$v['title']."/".stripcslashes($v['entitle'])."</option>";
				}
				$this->ajaxReturn($html);
			}
			if($type=='device'){  //计算配送时间和费用
				$map['cityid']   = $this->_param('cityid','intval',0);
				$map['restid']   = $this->_param('restid','intval',0);
				$map['addid']    = $this->_param('addid','intval',0);
				$map['pointlng'] = $this->_param('pointlng','trim','');
				$map['pointlat'] = $this->_param('pointlat','trim','');
				
				$data = $model->getDevice($map);
				$this->ajaxReturn($data);
			}
			if($type=='pointer'){
				$map['cityid']   = $this->_param('cityid','intval',0);
				$map['pointlng'] = $this->_param('pointlng','trim','');
				$map['pointlat'] = $this->_param('pointlat','trim','');
				$map['keywords'] = $this->_param('keywords','trim','');
				$map['cuisine']  = $this->_param('cuisine', 'intval','');
				
				$data = $model->getOutletArray($map);
				$this->ajaxReturn($data);
			}
			if($type=='asks'){
				$map['ask'] = $this->_param("ask",   "trim",    "");
				$map['id']  = $this->_param("cartid","intval",  0);
				sendRest("RMS/Cart/editask",$map);
			}else{
				$data = $model->addOrder($_POST);
				$this->ajaxReturn($data);
			}
		}else{
			clearSession();
			
			//免费饮料
			$list = F("Sysconfig");                         //系统配置
			$this->assign("sdrink",  C("IS_SEND_DRINK"));   //读取赠送的饮料信息
			$this->assign("isdrink",$list['cfg']['drink']); //读取饮料赠送权限
			
			$this->display();
		}
	}
	
	/**
	 * 添加订单-选择常用地址
	 */
	public function site(){
		
		if(IS_AJAX){
			
			$map['keywords'] = $this->_param('value','trim','');
			$map['cityid']   = $this->_param('cityid','intval',0);
			$json = sendRest("RMS/Site/select",$map);
			
			$html = "";
			foreach($json['data'] as $k=>$v){
				$html .= "<dd><span pointlng='".$v['longitude']."' pointlat='".$v['latitude']."' did=".$v['id'].">".$v['address']."</span></dd>";
			}
				
			$this->ajaxReturn($html);
		}else{
			
			$this->display();
		}
	}
	
	/**
	 * 通过地图选址
	 */
	public function map(){
		
		$cityid  = $this->_param('cityid','intval',0);
		
		//地址列表
		$dict_address = sendRest("BDC/Outlet/dictAddress");
		$this->assign("city",$dict_address['data']['zh-cn']['array'][$cityid][0]);
		
		$this->display();
	}
	
	/**
	 * 添加商品到购物车
	 */
	public function addcart(){
		
		$model = D('Order');
		
		$map['cityid']   = $this->_param('cityid','intval',0);
		$map['outletid'] = $this->_param('outletid','intval',0);
		$map['val']      = $this->_param('val','trim','');
		$map['flag']     = $this->_param('flag','trim','');
		
		if($map['val']){
			$data  = $model->addcart($map);
			$this->ajaxReturn($data);
		}else{
			$this->ajaxReturn(array('status'=>-1,'info'=>'您还没有选择任何菜品！'));
		}
	}

	/**
	 * 购物车列表
	 */
	public function cartlist(){
	
		$model = D('Order');
		$map   = getMemberSession();
		$map['admin']    = 1;
		$map['outletid'] = $this->_param('outletid','intval',0);
		$data  = sendRest('RMS/Cart/view',$map);
	
		$dishesfee = 0;
		$drinkfee  = 0;
		$totaltime = 0;
		$servefee  = $this->_param('servefee','intval',0);
		$html      = '<tr align="center"><td class="ele_title">菜品编号</td><td class="ele_title">菜品名称</td><td class="ele_title">菜品单价</td><td class="ele_title">数量</td>
				<td class="ele_title">小计</td><td class="ele_title">备注</td><td class="ele_title">商品属性</td><td class="ele_title">操作</td></tr>';
	
		foreach($data['data']['list'] as $k=>$v){

			if($v['flag'] == 'dishes'){
				$dishesfee += $v['xjprice'];
			}else{
				$drinkfee  += $v['xjprice'];
			}

			$gnfr  = json_decode($v['gnfr'],true);	
			$one   = sendRest('RMS/Product/detail',array('id'=>$v['dishesid']));
			$title = $v['gnfr']?'修改属性':'添加属性';
			$bixu  = $model->checkAttr($one['data']['gnfr']);
			$href  = $one['data']['gnfr']?"<a class='other' cid='".$v['id']."' dishesid='".$v['dishesid']."' id='addattr'>".$bixu.$title."</a>":"";
			$edit  = $v['gnfr']?"<a class='other selectattr' rel='show'>查看属性</a>":"";
				
			$html .= "<tr align='center'>";
			$html .= "<td>".$v['dishes_num']."</td>";
			$html .= "<td>".$v['title']."<br/>".$v['entitle']."</td>";
			$html .= "<td>￥".$v['price']."</td>";
			//$html .= "<td><ul class='count'><li id='".$v['id']."' rel='minus' class='editnum minus'>-</li>";
			//$html .= "<li id='num".$v['id']."' class='num'>".$v['num']."</li>";
			//$html .= "<li id='".$v['id']."' rel='plus' class='editnum plus'>+</li></ul></td>";
			$html .= '<td><input type="text" id="'.$v['id'].'" class="form_ele_text editnum" style="width:60px;text-align:center" name="pronum['.$v['id'].']" value="'.$v['num'].'"></td>';
			$html .= "<td>￥".$v['xjprice']."</td>";
			$html .= "<td><input type='text' class='form_ele_text width_150 asks' value='".$v['ask']."' cartid='".$v['id']."'></td>";
			$html .= "<td>$edit</td>";
			$html .= "<td width='170'><a class='delPro' id='".$v['id']."'>删除</a>&nbsp;$href</td>";
			$html .= "</tr>";
			$html .= $v['gnfr']?$model->getDishesAttr($gnfr):"";
				
			$totaltime += $v['dishes_time'];
		}

		$dishesfee = number_format($dishesfee,2);
		$drinkfee  = number_format($drinkfee,2);
		$totalfee  = number_format($dishesfee + $drinkfee + $servefee,2);
	
		//$html .= "<tr style='height:30px;'><td><input type='hidden' name='dishestime' value='".$totaltime."'></td></tr>";
		if($totalfee>0){
			$html .= "<input type='hidden' name='dishestime' value='".$totaltime."'></td>";
			$html .= '<tr><td colspan="6">&nbsp;</td>';
			$html .= '<td colspan="2">餐厅费用：￥'.$dishesfee.'</td>';
			$html .= '</tr>';
			$html .= '<tr><td colspan="6">&nbsp;</td>';
			$html .= '<td colspan="2">酒水费用：￥'.$drinkfee.'</td>';
			$html .= '</tr>';
			$html .= '<tr><td colspan="6">&nbsp;</td>';
			$html .= '<td colspan="2">服务费用：￥'.$servefee.'</td>';
			$html .= '</tr>';
			$html .= '</tr>';
			$html .= '<tr><td colspan="6">&nbsp;</td>';
			$html .= '<td colspan="2">总　　价：￥'.$totalfee.'</td>';
			$html .= '</tr>';
		}
		$this->ajaxReturn($html);
	}
	
	/**
	 * 购物车列表
	 */
	public function cartlist_bak(){
		
		$model = D('Order');
		$map   = getMemberSession();
		$map['outletid'] = $this->_param('outletid','intval',0);
		$data  = sendRest('RMS/Cart/view',$map);
		
		$totaltime = 0; 
		$html      = '<tr align="center"><td class="ele_title">菜品编号</td><td class="ele_title">菜品名称</td><td class="ele_title">菜品单价</td><td class="ele_title">数量</td>
				<td class="ele_title">小计</td><td class="ele_title">备注</td><td class="ele_title">商品属性</td><td class="ele_title">操作</td></tr>';
		
		foreach($data['data']['list'] as $k=>$v){
			
			$one   = sendRest('RMS/Product/detail',array('id'=>$v['dishesid']));
			$title = $v['gnfr']?'修改属性':'添加属性';
			$href  = $one['data']['gnfr']?"<a class='other' cid='".$v['id']."' dishesid='".$v['dishesid']."' id='addattr'>".$title."</a>":"";
			$edit  = $v['gnfr']?"<a class='other selectattr' rel='show'>查看属性</a>":"";
			$gnfr  = json_decode($v['gnfr'],true);
			
			$html .= "<tr align='center'>";
			$html .= "<td>".$v['dishes_num']."</td>";
			$html .= "<td>".$v['title']."<br/>".$v['entitle']."</td>";
			$html .= "<td>￥".$v['price']."</td>";
			$html .= "<td><ul class='count'><li id='".$v['id']."' rel='minus' class='editnum minus'>-</li>";
			$html .= "<li id='num".$v['id']."' class='num'>".$v['num']."</li>";
			$html .= "<li id='".$v['id']."' rel='plus' class='editnum plus'>+</li></ul></td>";
			$html .= "<td>￥".$v['xjprice']."</td>";
			$html .= "<td><input type='text' class='form_ele_text width_150 asks' value='".$v['ask']."' cartid='".$v['id']."'></td>";
			$html .= "<td>$edit</td>";
			$html .= "<td width='170'><a class='delPro' id='".$v['id']."'>删除</a>&nbsp;$href</td>";
			$html .= "</tr>";
			$html .= $v['gnfr']?$model->getDishesAttr($gnfr):"";
			
			$totaltime += $v['dishes_time'];
		}
		
		$html .= "<tr style='height:30px;'><td><input type='hidden' name='dishestime' value='".$totaltime."'></td></tr>";
		$this->ajaxReturn($html);
	}
	
	/**
	 * 删除购物车中某件商品
	 */
	public function delcart(){
		
		$map = getMemberSession();
		$map['id'] = $this->_param('id','intval',0);
		$data = sendRest('RMS/Cart/del',$map);
		
		$this->ajaxReturn($data);
	}
	
	/**
	 * 更新购物车中商品数量
	 */
	public function numcart(){
		
		$map['id']  = $this->_param('id', 'intval',0); //购物车id
		$map['num'] = $this->_param('num','intval',1); //数量
		
		$data = sendRest('RMS/Cart/setnum',$map);
		$this->ajaxReturn($data);
	}
	
	/**
	 * 添加商品属性
	 */
	public function attrcart(){
		
		$model = D('Order');
		if(IS_AJAX){
			
			$array = $_POST['flag'];
			$gnfr  = $model->arrray_remove($array);   //移除掉数组中值为空的数组
				
			//对数组进行排序
			ksort($gnfr);
			foreach ($gnfr as $k=>$v){
				ksort($gnfr[$k]['config']);
			}
				
			$map         = $model->getAttrTotal($gnfr);
			$map['gnfr'] = $gnfr;
			$map['id']   = $this->_param('id','intval',0);
			
			$data = sendRest('RMS/Cart/edit',$map,'post');
			$this->ajaxReturn($data);
		}else{
			
			$cid       = $this->_param('cid','intval',0);
			$dishesid  = $this->_param('dishesid','intval',0);
			
			//商品属性
			$map['id'] = $dishesid;
			$json = sendRest('RMS/Product/dishesDetail',$map);
			$data = $json['data'];
			
			$gnfr = json_decode($data['gnfr'],true);
			$data['gnfr'] = $gnfr;
			
			//已经选择的属性
			$view = sendRest('RMS/Cart/detail',array('id'=>$cid));
			$attr = json_decode($view['data']['gnfr'],true);
			$this->assign('attr',$attr);
			$this->assign('list',$data);
			$this->assign('cid',$cid);
			$this->display();
		}
	}
}

?>